#include "analyse.h"
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <cmath>

#include<QtDebug>
Analyse::Analyse()
{
}

//原始数据异常值线性插值方法
void Analyse::interpolation(double *p){
    if(end <=0){
        return;
    }
        for (int i = 0; i <= end; i++) {
            if (0 <= (*(p + i) - *(p + i - 1)) && (*(p + i) - *(p + i - 1) >= 500)) {
               double  m = (*(p + i) - *(p + i - 1)) /2;
                *(p + i) = *(p + i) + m ;
            }
        }
    }

//原始数据异常值标准差优化
double Analyse::standardDev(double *p){
    double sum=0;
    double avg = 0;
    double fangcha = 0;
    for(int i=1;i<=leh;i++){
        sum += *(p+i);
    }
    avg = sum/leh;
    for(int i=1;i<=leh;i++){
        sum += pow(*(p+i)-avg,2);
    }
    fangcha = sum/leh;

    return sqrt(fangcha);
}


/*  暂时不用此方法处理荧光曲线
//计算原始荧光曲线数据的B序列方法
double Analyse::yingB(int *Fla ,double *Flb,int len )
{
    leh = len;
    int  i ,t1,t2;
    int k[3];
    double *pf, f1,f2,f3,f4 ,tmp;
    p = Fla ;
    pf = Flb;
    if(*p <= 0)
    {
        for(i=1;i<len;i++)
        {
            if(*(p+i)>0)
            {
                *p = *(p+i) ;
                i = len ;
            }
        }
    }
    if(*(p+len-1)<=0)
    {
        for(i=(len-1) ;i>0;i--)
        {
            if(*(p+i)>0)
            {
                *(p+len-1) = *(p+i) ;
                i = 0 ;
            }
        }
    }
    for(i = 1;i<(len-1);i++)
    {
        if(*(p+i)<=0)
        {
            *(p+i) = (*(p+i-1)  + *(p+i+1))/2 ;
        }
    }
    k[0] = *p ;
    k[1] = *(p+1) ;
//    for(i=2;i<len;i++)//滤波窗口 0.2--0.8
//    {
//        k[2] = *(p+i) ;
//        t1 = (k[0] + k[2])*4/8 ;
//        t2 = (k[0] + k[2])/5;
//        if((k[1]>t1)||(k[1]<t2))
//        {
//            k[1]  = (k[0] + k[2])/2 ;
//            *(p+i-1) = k[1] ;
//        }
//    }

    pf++;
    tmp = 0 ;
    f1 = log10(*p) ;
    f3 = log10(1);
    int avg = avgEnd-avgStart+1;
    for(i=0;i<(len-1);i++)
    {
        f2 = log10(*(p+i+1)) ;
        f4 = log10(i+2) ;
        *(pf+i) = (f1-f2)/(f4-f3) ;
        //qDebug()<<*(pf+i);
        if(i<avg)
        {
            tmp = tmp + *(pf+i);
        }
        f1 = f2 ;
        f3 = f4 ;
    }
    return  tmp/avg;
}

//计算A序列荧光值和平均值
double Analyse::yingA(double *FLa, double *FHa){
    double yingAPow;
    double temp=0;
    int avg = avgEnd-avgStart+1;
    for(int i=1;i<=leh;i++){
        yingAPow = pow(i,*(FLa+i));
        *FHa = yingAPow*(*(p+i-1));

        if(i<=avg){
           temp = temp + *FHa;
        }
        FHa++;
    }
    return temp/avg; //计算1-7个循环的平均数
}

//计算Y拟合函数
double Analyse::yingN(double *yingN, double yingAAvg, double yingBAvg){
    int avg = 17;
    double temp;
    double yingNPow;
    int tempAvg = leh - avg +1;
    for(int i=1;i<=leh;i++){
        yingNPow = pow(i,yingBAvg);
        *yingN = yingAAvg/yingNPow;

        if(i>=avg){
            temp = temp + *yingN;
        }
        yingN++;
    }
    return  temp/tempAvg;

}


//计算每组数组的衰减系数
double Analyse::yingX(double *YingNF,double *YingX, double yingNAvg){

    for(int i=1;i<=leh;i++){
        *YingX = yingNAvg/(*(YingNF+i-1));
        YingX++;

    }
    return 1;
}

//计算每组数据衰减后的荧光曲线
double Analyse::yingJ(double *YingX, double *YingFJ){

    for(int i=1;i<=leh;i++){
        *YingFJ = (*(p+i-1))*(*(YingX+i-1));
        YingFJ++;
    }
    return 1;
}
*/

//原始荧光数据不执行衰减，去掉前三个循环荧光数值计算新的荧光数据
double Analyse::yingBase(double *YingBase){

    double yingBaseSum=0;
    for(int i=start;i<=end;i++){
        yingBaseSum += *(p+i-1);
    }
    //qDebug()<<*p;
    double yingBaseAvg = yingBaseSum/(end-start+1);
    //qDebug()<<yingBaseAvg;
    for(int i=0;i<start;i++){
        *(p+i) = yingBaseAvg;
    }
    for(int i=0;i<leh;i++){
         *YingBase = *(p+i);

         *YingBase++;
    }
    return 1;
}

//原始荧光数据不执行衰减，前三个循环荧光数值第四个荧光数据一致
double Analyse::yingFour(double *YingFour){

    double yingFour = *(YingFour + 3);  // 使用参数 YingFour，修正指针名称
    for (int i = 1; i < start; i++) {  // 修正循环条件
        *(YingFour + 1) = yingFour;  // 将 yingFour 赋给 *(YingFour + 1)
    }
    // qDebug()<<*YingFour;
    return 1;
}

//计算SG三阶5点滤波
double Analyse::IIR_Init(double *Fla,double *Flb,double IIR_c[5][5])
{
    double *p ,*pf ;
    double k1 ,k2,k3,k4,k5 ;
    p = Fla ;
    pf = Flb ;

    for(int i = 2;i<(leh-2);i++)
    {
        k1 = IIR_c[0][2]*(*(pf+ i - 2));
        k2 = IIR_c[1][2]*(*(pf+ i - 1));
        k3 = IIR_c[2][2]*(*(pf+ i));
        k4 = IIR_c[3][2]*(*(pf+ i + 1));
        k5 = IIR_c[4][2]*(*(pf+ i + 2));
        *(p+i) = k1 + k2 + k3 + k4 + k5 ;
    }
    k1 = IIR_c[0][0]*(*(pf+0));
    k2 = IIR_c[1][0]*(*(pf+1));
    k3 = IIR_c[2][0]*(*(pf+2));
    k4 = IIR_c[3][0]*(*(pf+3));
    k5 = IIR_c[4][0]*(*(pf+4));
    *p = k1 + k2 + k3 + k4 + k5 ;
    k1 = IIR_c[0][1]*(*(pf+0));
    k2 = IIR_c[1][1]*(*(pf+1));
    k3 = IIR_c[2][1]*(*(pf+2));
    k4 = IIR_c[3][1]*(*(pf+3));
    k5 = IIR_c[4][1]*(*(pf+4));
    *(p+1) = k1 + k2 + k3 + k4 + k5 ;
    k1 = IIR_c[0][3]*(*(pf+leh-5));
    k2 = IIR_c[1][3]*(*(pf+leh-4));
    k3 = IIR_c[2][3]*(*(pf+leh-3));
    k4 = IIR_c[3][3]*(*(pf+leh-2));
    k5 = IIR_c[4][3]*(*(pf+leh-1));
    *(p+leh-2) = k1 + k2 + k3 + k4 + k5 ;
    k1 = IIR_c[0][4]*(*(pf+leh-5));
    k2 = IIR_c[1][4]*(*(pf+leh-4));
    k3 = IIR_c[2][4]*(*(pf+leh-3));
    k4 = IIR_c[3][4]*(*(pf+leh-2));
    k5 = IIR_c[4][4]*(*(pf+leh-1));
    *(p+leh-1) = k1 + k2 + k3 + k4 + k5 ;
    return 1 ;
}

//计算SG 二阶7点滤波
double Analyse::II_Seven(double *Fla,double *Flb,double IIR_c[7][7])
{
    double *p ,*pf ;
    double k1 ,k2,k3,k4,k5,k6,k7 ;
    p = Fla ;
    pf = Flb ;

    for(int i = 3;i<(leh-3);i++)
    {
        //qDebug()<<"diyi1:"<<*(pf+ i);
        k1 = IIR_c[0][3]*(*(pf+ i - 3));
        k2 = IIR_c[1][3]*(*(pf+ i - 2));
        k3 = IIR_c[2][3]*(*(pf+ i - 1));
        k4 = IIR_c[3][3]*(*(pf+ i));
        k5 = IIR_c[4][3]*(*(pf+ i + 1));
        k6 = IIR_c[5][3]*(*(pf+ i + 2));
        k7 = IIR_c[6][3]*(*(pf+ i + 3));
        *(p+i) = k1 + k2 + k3 + k4 + k5+ k6 + k7 ;

    }
    k1 = IIR_c[0][0]*(*(pf+0));
    k2 = IIR_c[1][0]*(*(pf+1));
    k3 = IIR_c[2][0]*(*(pf+2));
    k4 = IIR_c[3][0]*(*(pf+3));
    k5 = IIR_c[4][0]*(*(pf+4));
    k6 = IIR_c[5][0]*(*(pf+5));
    k7 = IIR_c[6][0]*(*(pf+6));
    *p = k1 + k2 + k3 + k4 + k5+ k6 + k7 ;

    k1 = IIR_c[0][1]*(*(pf+0));
    k2 = IIR_c[1][1]*(*(pf+1));
    k3 = IIR_c[2][1]*(*(pf+2));
    k4 = IIR_c[3][1]*(*(pf+3));
    k5 = IIR_c[4][1]*(*(pf+4));
    k6 = IIR_c[5][1]*(*(pf+5));
    k7 = IIR_c[6][1]*(*(pf+6));
    *(p+1) = k1 + k2 + k3 + k4 + k5+ k6 + k7 ;

    k1 = IIR_c[0][2]*(*(pf+0));
    k2 = IIR_c[1][2]*(*(pf+1));
    k3 = IIR_c[2][2]*(*(pf+2));
    k4 = IIR_c[3][2]*(*(pf+3));
    k5 = IIR_c[4][2]*(*(pf+4));
    k6 = IIR_c[5][2]*(*(pf+5));
    k7 = IIR_c[6][2]*(*(pf+6));
    *(p+2) = k1 + k2 + k3 + k4 + k5+ k6 + k7 ;

    k1 = IIR_c[0][4]*(*(pf+leh-7));
    k2 = IIR_c[1][4]*(*(pf+leh-6));
    k3 = IIR_c[2][4]*(*(pf+leh-5));
    k4 = IIR_c[3][4]*(*(pf+leh-4));
    k5 = IIR_c[4][4]*(*(pf+leh-3));
    k6 = IIR_c[5][4]*(*(pf+leh-2));
    k7 = IIR_c[6][4]*(*(pf+leh-1));
    *(p+leh-3) = k1 + k2 + k3 + k4 + k5+ k6 + k7 ;

    k1 = IIR_c[0][5]*(*(pf+leh-7));
    k2 = IIR_c[1][5]*(*(pf+leh-6));
    k3 = IIR_c[2][5]*(*(pf+leh-5));
    k4 = IIR_c[3][5]*(*(pf+leh-4));
    k5 = IIR_c[4][5]*(*(pf+leh-3));
    k6 = IIR_c[5][5]*(*(pf+leh-2));
    k7 = IIR_c[6][5]*(*(pf+leh-1));
    *(p+leh-2) = k1 + k2 + k3 + k4 + k5+ k6 + k7 ;

    k1 = IIR_c[0][6]*(*(pf+leh-7));
    k2 = IIR_c[1][6]*(*(pf+leh-6));
    k3 = IIR_c[2][6]*(*(pf+leh-5));
    k4 = IIR_c[3][6]*(*(pf+leh-4));
    k5 = IIR_c[4][6]*(*(pf+leh-3));
    k6 = IIR_c[5][6]*(*(pf+leh-2));
    k7 = IIR_c[6][6]*(*(pf+leh-1));
    *(p+leh-1) = k1 + k2 + k3 + k4 + k5+ k6 + k7 ;

    return 1 ;
}
//计算基线数值（滤波后数组3-15个循环数值的平均值）
double Analyse::lvboAVG(double *YingLvbo){
    double yingLvboSum;
    for(int i=start;i<=end;i++){
        yingLvboSum += *(YingLvbo);
        YingLvbo++;
    }
    yingJAvg = yingLvboSum/(end-start+1);
    return yingJAvg;
}
//计算delta 荧光数值
double Analyse::delta(double *yingLvbo,double *jixianF,double yingJAvg){
    for(int i=1;i<=leh;i++){
        *jixianF = *(yingLvbo+i)-yingJAvg;
        jixianF++;
    }
    return 1;
}
//计算自动均值基线FDM
double Analyse::yingFDM(double *YingLvbo,double *FDM){

   int maxDiff = *(YingLvbo+1)-*(YingLvbo);
   int maxIndex =1; //初始化最大差值索引为1
    for(int i=1;i<=leh;i++){
        int currentDiff = *(YingLvbo+i+1)-*(YingLvbo+i);
        qDebug()<<"滤波后数据"<< *(YingLvbo+i)<<"滤波后减1数" <<*(YingLvbo+i-1);
        if(currentDiff>maxDiff){
            maxDiff=currentDiff;
            maxIndex = i+1;//更新最大差值索引
        }
        *FDM=currentDiff; //将当前差值存储到FDM数组中
        FDM++;//移动指针到下一个位置
    }
    c0=maxIndex;//更新C0为最大差值的索引
    qDebug()<<"maxFDM"<< c0 << maxDiff;
    return maxDiff;
}
//计算自动均值基线SDM
double Analyse::yingSDM(double *FMD,double *SDM){

    int maxDiff = *SDM;
    int maxIndex =0;

     for(int i=0;i<leh-3;i++){
         *SDM = *(FMD+i+1)-*(FMD+i);
         //qDebug()<<"1:"<< *(FMD+i+1)<<"2:" <<*(FMD+i)<<"3:" << *SDM;
         if(*SDM>maxDiff){
             maxDiff=*SDM;
             maxIndex = i;
         }
         SDM++;
     }
     c0SDM = maxIndex;
     //qDebug()<<"c0SDM"<< max;
     return maxDiff;
}

//计算扩增斜率K
double Analyse::yingK(double e){
    //qDebug()<<"xiaolv"<< e;
    k= 1/(log(1+e)/log(10)); //根据扩增效率求斜率 以10为底的对数计算
    return k;
}

//计算CT值
double Analyse::yingCT(double k, double CFDM, double Rmax, double Rjixian){
    double ct =CFDM-k*log(Rmax/Rjixian-1);
    return ct;
}

//计算CT值-阈值线交叉法
double Analyse::yingCTJiao(double *yingLvbo, double yuzhi, int end){

    double xiaoYuzhi=0;
    double daYuzhi=0;
    double CtJ;
    for(int i=end;i<leh;i++){
        if(*(yingLvbo+i)>=yuzhi){

          xiaoYuzhi = *(yingLvbo+i)-*(yingLvbo+i-1);
          daYuzhi = yuzhi - *(yingLvbo+i-1);
          CtJ = i+daYuzhi/xiaoYuzhi;
          i=leh;
        }
    }
    return CtJ;
}

//计算CT值-基于sigmoid 拟合函数
double Analyse::yingCTSigmoid(double yuzhi, double beijing,double k, double maxLvbo){
    double CTSigmoid=0;
    double tema = (maxLvbo-beijing)/(yuzhi-beijing)-1;
    double tem = log(tema);
    CTSigmoid = leh-tem*k;

    return CTSigmoid;
}
double Analyse::Exp_Curve(double *Fla ,double *Rn,double Scope,int C0,int Cm)
{
    double tmp1 ,tmp2  ;
    double *p ,*pf ,Fmax;
    p = Fla ;
    pf = Rn ;
    if(C0>=Cm)
    {
        return 0 ;
    }

    Fmax = *(p+Cm) ;
    for(int i =0 ;i<=C0;i++)
    {
        *(p+i) = *(pf+i) ;
    }
    for(int i = C0 ;i<Cm;i++)
    {
        tmp1 = C0 - (i+1)/Scope ;
        tmp2 = exp(tmp1) +1  ;
        tmp1 = Fmax/tmp2 + *(pf + i +1) ;
        *(p+i+1) = tmp1 ;
    }
    return 1 ;
}

//计算数值偏差S 阈值计算
double Analyse:: xunhuanS(double *yingLvbo,int start, int end){
    double sum = 0;
    int xunhuanSum = end - start+1;
    for(int i=start;i<=end;i++){
       sum += *(yingLvbo+i);
      //  qDebug() <<"3-15循环滤波："<< *(yingLvbo+i);
    }
    XAvg = sum/xunhuanSum;

    double xSUM = 0;
    //double xSUMAvg=0;
    double s=0;
    for(int i=start;i<=end;i++){
    //    qDebug() <<"*(yingLvbo+i)"<< *(yingLvbo+i);
        xSUM += pow((*(yingLvbo+i)-XAvg),2);
    }

    LvStd = xSUM/xunhuanSum;
    LvStd = sqrt(LvStd);
    if(LvStd<100){
        s = LvStd*10+XAvg;//10倍偏差+基线平均值
    }else{
        s = LvStd+XAvg; //标准偏差+基线平均值
    }
    //s = sqrt(xSUMAvg)+XAvg; //标准偏差+基线平均值
    //s = sqrt(xSUMAvg)*10+XAvg;//10倍偏差+基线平均值
    //s = sqrt(xSUMAvg)*10;//10倍偏差
    std = LvStd; //页面显示标准偏差数值
    //qDebug() <<"yinglvbo"<< std;
    return s;
}
//计算扩增效率和斜率
double Analyse::K_slope(double *Fla,double *Flb,int len,int m)
{
    double *p,*pf;
    std::vector<double> tmp(len);
    double k ;
    int n ;
    p = Fla ;
    pf = Flb ;
    for(int i=0 ;i<len;i++)
    {
        tmp[i] = p[i] - pf[i];
        if(tmp[i]<0)
        {
            tmp[i] = 0 ;
        }
    }
    if(m<3)
    {
        m = 3;
    }
    for( int i=0;i<(len-1);i++)
     {
        if((tmp[i]>0)&&(tmp[i+1]>0))
        {
            tmp[i] = p[i+1] - p[i] ;
            if(tmp[i]<0)
            {
              tmp[i] =0 ;
            }
         }
     }
    for(int j=1 ;j<m;j++)
    {

       n = 0 ;
       for( int i=0;i<(len-1+j-m);i++)
       {
          for(int a=0;a<(m-j);a++)
          {
              if(tmp[i+a]<=0)
              {
                tmp[i] = 0 ;
              }
          }

       }
       for( int i=0;i<(len-1-j);i++)
        {
           if((tmp[i]>0)&&(tmp[i+1]>0))
           {
               tmp[i] = tmp[i+1] - tmp[i] ;
               if(tmp[i]<0)
               {
                 tmp[i] =0 ;
               }
            }
        }

    }
    for(int i=0;i<(len-m);i++)
    {
        if(tmp[i]>0)
        {
            n = i ;
            i = len ;
        }
    }
    k = *(p+n + m) / *(p+n);
    k = (log(k))/m ;
    k = pow(10,k);
    return k ;

}

//根据最新拟合后的数据计算效率和斜率
double Analyse::eff(double *Fla,double *Flb,int len,int m)
{
    double *p,*pf;
    double *tmp = new double[len] ;
    double k ;
    int n ;
    p = Fla ;
    pf = Flb ;
    for(int i=0 ;i<len;i++)
    {
        tmp[i] = p[i] - pf[i];
        if(tmp[i]<0)
        {
            tmp[i] = 0 ;
        }
    }
    if(m<3)
    {
        m = 3;
    }
    for( int i=0;i<(len-1);i++)
     {
        if((tmp[i]>0)&&(tmp[i+1]>0))
        {
            tmp[i] = p[i+1] - p[i] ;
            if(tmp[i]<0)
            {
              tmp[i] =0 ;
            }
         }
     }
    for(int j=1 ;j<m;j++)
    {

       n = 0 ;
       for( int i=0;i<(len-1+j-m);i++)
       {
          for(int a=0;a<(m-j);a++)
          {
              if(tmp[i+a]<=0)
              {
                tmp[i] = 0 ;
              }
          }

       }
       for( int i=0;i<(len-1-j);i++)
        {
           if((tmp[i]>0)&&(tmp[i+1]>0))
           {
               tmp[i] = tmp[i+1] - tmp[i] ;
               if(tmp[i]<0)
               {
                 tmp[i] =0 ;
               }
            }
        }

    }
    for(int i=0;i<len-m;i++)
        if(tmp[i]>0)
        {
            n = i ;
            break ;
        }
    if(n+m<len){
        k = *(p+n + m) / *(p+n);
        k = (log(k))/m ;
        k = pow(10,k);
    }else{
        k=0;
    }
   delete[]tmp;
    return k ;

}

//计算滤波后的归一化方法
double Analyse::normalize(double *yingLvbo, double *narm){
    double *lvtemp;
    lvtemp = yingLvbo;

    int max , min = *lvtemp;
    for(int i=0;i<leh;i++){
        if(*lvtemp>max){
            max=*lvtemp;
        }
        if(*lvtemp<min){
            min=*lvtemp;
        }
        lvtemp++;
    }

    for(int i=0;i<=leh;i++){
        double temp1 = *(yingLvbo+i)-min;
        *narm= temp1/(max-min);
        narm++;
    }
    return 1;
}

//计算拟合后的归一化方法
double Analyse::normalizeN(double *nihe, double *narm){
    double *lvtemp;
    lvtemp = nihe;

    int max= *lvtemp ;
    int min = *lvtemp;

    for(int i=0;i<leh;i++){
       if(lvtemp !=nullptr){
           if(*lvtemp>max){
               max=*lvtemp;
           }
           if(*lvtemp<min){
               min=*lvtemp;
           }
            lvtemp++;
       }else{
        lvtemp++;
       }
    }

    for(int i=0;i<leh;i++){
        double temp1 = *(nihe+i)-min;
        *narm= temp1/(max-min);
        narm++;
    }
    return 1;
}
//滤波后根据SIGMOD拟合函数计算拟合后曲线
double Analyse::sigmod(double *yingLvbo,double k, double *yingSigmod){
    int maxLvbo = *yingLvbo;
    double e = 2.71828;
    int xunhuanshu =end-start+1;

    double sum = 0;//初始化sum
    for(int i=0;i<=leh;i++){
        qDebug() << "滤波后" << *(yingLvbo+i);
        if(*(yingLvbo+i)>maxLvbo){
            maxLvbo=*(yingLvbo+i);
        }
        if(i>=start&&i<=end){
             sum = sum + *(yingLvbo+i);
        }
       // yingLvbo++;
    }
    yingJAvg = sum/xunhuanshu;
    qDebug() << yingJAvg;
    /*if(c0<10){
        for(int i=0;i<leh;i++){
            *(yingSigmod+i) = *(yingLvbo+i-1)-yingJAvg; //相差很少的数据处理
             if(*(yingSigmod+i)<500){
                 *(yingSigmod+i) =0;
             }
            //qDebug() << "oldlvbo:" << *(yingLvbo+i);
        }
    }else{*/
        for(int i=0;i<leh;i++){
           double zhishu =  (c0-i)/k;
           double dishu = 1+pow(e,zhishu);
           *yingSigmod = yingJAvg+maxLvbo/dishu; //有基线值显示
           //*yingSigmod = maxLvbo/dishu; //去掉基线值显示
           qDebug() << *yingSigmod;
            yingSigmod++;
        }

   // }
    return 1.0;
}
//滤波后根据SIGMOD拟合函数计算拟合后曲线
double Analyse::sigmodDelta(double maxLvbo,double *yingLvbo,double k, double *yingSigmod, double d){

    Analyse::LinearFit(yingLvbo);

//    int maxLvbo = *yingLvbo;

    double e = 2.71828;
    int xunhuanshu =end-start+1;
    //double d = 1;
    //double jixian;

    double sum=0;
    double sumYing=0;
    for(int i=start;i<=end;i++){
        sumYing += *(yingLvbo+i-1);
    }
    for(int i=1;i<=leh;i++){
        sumYing+= *(yingLvbo+i-1);
    }
    double sumYingAVG = sumYing/leh;
    for(int i=1;i<=leh;i++){
        sumYing +=  pow(*(yingLvbo+i-1)-sumYingAVG,2);
    }
    double LvStdYing = sqrt(sumYing/(leh-1));//滤波后数据的标准偏差
//    yingJAvg = sum/xunhuanshu;
    double cying = (maxLvbo +yingJAvg)/2;
    double c00 = (cying-maxB)/maxK;
    //qDebug() << yingJAvg;
    if((c00<10&&c00>40)||LvStdYing<100){

        for(int i=0;i<leh;i++){
            *(yingSigmod+i) = 0;
           //*(yingSigmod+i) = *(yingLvbo+i-1)-yingJAvg; //相差很少的数据处理
            // qDebug() << "123:" << *(yingSigmod+i);
           // if(*(yingSigmod+i)<500){
            //    *(yingSigmod+i) =0;
           // }
            //yingSigmod++;
            //qDebug() << "456:" << *(yingSigmod+i);
        }
    }else{
        for(int i=1;i<=leh;i++){
           //double zhishu =  (c0-i)/k;
           double zhishu =  (c00-i)/k;
           double dishu = 1+pow(e,zhishu);
           double dishuD = pow(dishu,d);
           *yingSigmod = (maxLvbo-yingJAvg)/dishuD; //最大荧光值减去基线值后在计算 同时增加D偏差校正
           //*yingSigmod = yingJAvg+maxLvbo/dishu; //有基线值显示
           //yingSigmod = maxLvbo/dishu; //去掉基线值显示
           //qDebug() << *(yingSigmod+1);
            yingSigmod++;
        }
    }
    return 1;
}
//计算delta 荧光数值
double Analyse::logarithm(double *RnYing,double *RnYingF){
    double *logar;
    logar = RnYing;

    for(int i=1;i<leh;i++){
        double d = *(logar+i);
        //qDebug()<<d;
        *RnYingF = log10(d);
        RnYingF++;
    }
    return 1;
}
double Analyse::LinearFit(double *yingLvbo){
    double *s = yingLvbo;

    int chuang=3;

    double avgSDM=0;
    double avgS=0;
    double r2[3]={0};
    double k[3]={0};
    double b[3]={0};

    for(int z=0;z<chuang;z++){

        double sumS =0;
        int sumSDM=0;
        for(int i=c0+z-1;i<c0+z+2;i++){
            sumS +=*(s+i);
            sumSDM +=i;
        }
        avgS = sumS/chuang; //荧光值的平均值
        avgSDM = sumSDM/chuang; //循环数的平均值

        double sumFz=0;
        double sumFm=0;


        for(int i=c0+z-1;i<c0+z+2;i++){
            sumFz +=(i-avgSDM)*(*(s+i)-avgS);
            sumFm +=pow(i-avgSDM,2);
        }
        k[z] = sumFz/sumFm; //计算斜率k
        b[z] = avgS-k[z]*avgSDM; //计算常数b值
        double yy[3]={0};
        for(int i=0;i<chuang;i++){ //计算预测值
           yy[i]=k[z]*(c0+i-1+z)+b[z];
        }
        double rFz=0;
        double rFm=0;

        for(int i=0;i<chuang;i++){
            rFz += pow(yy[i]-avgS,2);
        }
        for(int i=c0+z-1;i<c0+z+2;i++){
            rFm += pow(*(s+i)-avgS,2);
        }

        r2[z]= rFz/rFm;
    }

    double max=r2[0];
    int a=0;

    for(int i=0;i<chuang;i++){
        if(r2[i]>max&&r2[i]<0.99999){
            rr2=r2[i];
            a=i;
        }
    }
    for(int i=0;i<chuang;i++){ //根据最大r2查找斜率k值
        if(i==a){
            maxK=k[i];

        }
        if(i==a){ //根据最大r2查找参数B值
            maxB=b[i];
        }
    }


    return 1;
}

//计算标准曲线相关计算结果 效率 斜率 截距 R²等
double Analyse::Standards(double ctArr[], double conArr[],int size){
    //计算斜率
    double sum_xy = 0.0;
    double sum_x = 0.0;
    double sum_y = 0.0;
    double sum_x_squared = 0.0;

    for (int i = 0; i < size; i++) {
        conArr[i]=log(conArr[i]);
        sum_xy += conArr[i] * ctArr[i];
        sum_x += conArr[i];
        sum_y += conArr[i];
        sum_x_squared += conArr[i] * conArr[i];


    }


    double numerator = size * sum_xy - sum_x * sum_y;
    double denominator = size * sum_x_squared - sum_x * sum_x;

    if (denominator != 0) {
        standardK = numerator / denominator;
        standardK = round(standardK*1000)/1000.0;

    } else {
        // 处理除数为0的情况
        return 0;
    }
    //计算截距
    double yPing = sum_y/size;
    double xPing = sum_x/size;
    standardYInt = yPing - k*xPing;
    standardYInt = round(standardYInt*1000)/1000.0;

    //计算效率
    efficiency = ((pow(10,(-1/k)))-1)*100;
    efficiency = round(efficiency*10)/10.0;

    //计算R²
    double sumFenzi=0.0;
    double sumFenmu=0.0;

    // 计算分母
    for (int i = 0; i < size; i++) {
        sumFenmu += pow(ctArr[i]-yPing,2);
    }

    // 计算分子
    for (int i = 0; i < size; i++) {
        //sumXY += (y[i]-yPing) * (x[i]-xPing);
       sumFenzi += pow(conArr[i]*k+standardYInt-yPing,2);//计算预测值
    }


    // 计算R²
    rSquared =sumFenzi / sumFenmu;
     rSquared = round(rSquared*10)/10.0;

    return 0;
}


//计算未知样品浓度
std::vector<double> Analyse::Concentration(double ctUnkownArr[], int size){
    std::vector<double> conUnkownArr;

    for(int i=0;i<size;i++){
        conUnkownArr.push_back((ctUnkownArr[i]-standardYInt)/standardK);
    }
    return conUnkownArr;
}


//计算抑制情况
double  Analyse::allmethod(double roxn, double vicn, double famn, QVector<double> cq, double cqv)
{
    manQuantity = famn;
    womanQuantity = vicn-1.39*famn;
    Quantityb = (vicn-1.39*famn)/famn;
    DI = vicn/roxn;
    double cqall = 0;
    int num = 0;
    for(int i=0;i<cq.count();i++){
        if(cq[i]!=NULL){
            cqall+=cq[i];
            num++;
        }
    }
    ICS = (cqv-cqall/num);
    return 0;
}
